#!/bin/bash

mcEngine="TGeant3"

echo "Supported data formats: binary, boost, boost-text, flatbuffers, msgpack, protobuf, tmessage"
dataFormat="binary"
if [ "$1" = "binary" ]; then
    echo "Using: binary"
elif [ "$1" = "boost" ]; then
    dataFormat="boost"
    echo "Using: boost (Boost binary)"
elif [ "$1" = "flatbuffers" ]; then
    if(@FLATBUFFERS_USED@); then
        dataFormat="flatbuffers"
        echo "Using: flatbuffers (Google FlatBuffers)"
    else
        echo "Cannot use flatbuffers: library not found at build time"
        exit 1
    fi
elif [ "$1" = "msgpack" ]; then
    if(@MSGPACK_USED@); then
        dataFormat="msgpack"
        echo "Using: msgpack (MessagePack)"
    else
        echo "Cannot use msgpack: library not found at build time"
        exit 1
    fi
elif [ "$1" = "protobuf" ]; then
    if(@PROTOBUF_USED@); then
        dataFormat="protobuf"
        echo "Using: protobuf (Google Protocol Buffers)"
    else
        echo "Cannot use protobuf: library not found at build time"
        exit 1
    fi
elif [ "$1" = "tmessage" ]; then
    dataFormat="tmessage"
    echo "Using: tmessage (Root TMessage)"
else
    echo "None or incorrect data format provided!"
    echo "Using: binary"
fi

SAMPLER="tut3-sampler"
SAMPLER+=" --id sampler1"
SAMPLER+=" --data-format $dataFormat"
SAMPLER+=" --severity info"
SAMPLER+=" --chain-input 99"
SAMPLER+=" --input-file @CMAKE_SOURCE_DIR@/examples/advanced/Tutorial3/macro/data/testdigi_$mcEngine.root"
SAMPLER+=" --parameter-file @CMAKE_SOURCE_DIR@/examples/advanced/Tutorial3/macro/data/testparams_$mcEngine.root"
SAMPLER+=" --channel-config name=data1,type=push,method=connect,address=tcp://localhost:5565"
SAMPLER+="                  name=ack,type=pull,method=bind,address=tcp://localhost:5999"
xterm -geometry 80x23+0+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$SAMPLER &

PROXY1="fairmq-proxy"
PROXY1+=" --id proxy1"
PROXY1+=" --in-channel data1"
PROXY1+=" --out-channel data2"
PROXY1+=" --channel-config name=data1,type=pull,method=bind,address=tcp://localhost:5565"
PROXY1+="                  name=data2,type=push,method=bind,address=tcp://localhost:5566"
xterm -geometry 80x23+0+350 -hold -e @FairMQ_BINDIR@/$PROXY1 &

PROCESSOR1="tut3-processor"
PROCESSOR1+=" --id processor1"
PROCESSOR1+=" --in-channel data2"
PROCESSOR1+=" --out-channel data3"
PROCESSOR1+=" --data-format $dataFormat"
PROCESSOR1+=" --channel-config name=data2,type=pull,method=connect,address=tcp://localhost:5566"
PROCESSOR1+="                  name=data3,type=push,method=connect,address=tcp://localhost:5567"
xterm -geometry 80x23+500+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$PROCESSOR1 &

PROCESSOR2="tut3-processor"
PROCESSOR2+=" --id processor2"
PROCESSOR2+=" --in-channel data2"
PROCESSOR2+=" --out-channel data3"
PROCESSOR2+=" --data-format $dataFormat"
PROCESSOR2+=" --channel-config name=data2,type=pull,method=connect,address=tcp://localhost:5566"
PROCESSOR2+="                  name=data3,type=push,method=connect,address=tcp://localhost:5567"
xterm -geometry 80x23+500+350 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$PROCESSOR2 &

PROCESSOR3="tut3-processor"
PROCESSOR3+=" --id processor3"
PROCESSOR3+=" --in-channel data2"
PROCESSOR3+=" --out-channel data3"
PROCESSOR3+=" --data-format $dataFormat"
PROCESSOR3+=" --channel-config name=data2,type=pull,method=connect,address=tcp://localhost:5566"
PROCESSOR3+="                  name=data3,type=push,method=connect,address=tcp://localhost:5567"
xterm -geometry 80x23+500+700 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$PROCESSOR3 &

PROXY2="fairmq-proxy"
PROXY2+=" --id proxy2"
PROXY2+=" --in-channel data3"
PROXY2+=" --out-channel data4"
PROXY2+=" --channel-config name=data3,type=pull,method=bind,address=tcp://localhost:5567"
PROXY2+="                  name=data4,type=push,method=bind,address=tcp://localhost:5570"
xterm -geometry 80x23+1000+0 -hold -e @FairMQ_BINDIR@/$PROXY2 &

FILESINK="tut3-sink"
FILESINK+=" --id sink1"
FILESINK+=" --in-channel data4"
FILESINK+=" --data-format $dataFormat"
FILESINK+=" --channel-config name=data4,type=pull,method=connect,address=tcp://localhost:5570"
FILESINK+="                  name=ack,type=push,method=connect,address=tcp://localhost:5999"
xterm -geometry 80x23+1000+350 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$FILESINK &